package test_new;

import java.util.Scanner;

public class TransposeMatrix {

	public static void main(String[] args) {
		int[][] matrixResult = matrixCreate();
		if (matrixResult.length == matrixResult[0].length){
			matrixRotation(matrixResult);
		} else {
			matrixRotation(matrixResult);
			matrixTranspose(matrixResult);
		}
			
		}


	private static void matrixRotation(int[][] matrixResult) {
		for (int i = 0; i < Math.abs(matrixResult.length/2); i++) {
				for(int j = i; j < matrixResult.length-1-i; j++) {
					int resultTemp = matrixResult[i][j];
					matrixResult[i][j] = matrixResult[matrixResult.length-1-j][i];
					matrixResult[matrixResult.length-1-j][i] = matrixResult[matrixResult.length-1-i][matrixResult.length-1-j];
					matrixResult[matrixResult.length-1-i][matrixResult.length-1-j] = matrixResult[j][matrixResult.length-1-i];
					matrixResult[j][matrixResult.length-1-i] = resultTemp;
					/*int resultTemp = matrixResult[i][j];
					matrixResult[i][j] = matrixResult[j][matrixResult.length-1-i];
					matrixResult[j][matrixResult.length-1-i] = matrixResult[matrixResult.length-1-i][matrixResult.length-1-j];
					matrixResult[matrixResult.length-1-i][matrixResult.length-1-j] = matrixResult[matrixResult.length-1-j][i];
					matrixResult[matrixResult.length-1-j][i] = resultTemp;*/
			}
		}
		showMatrix(matrixResult);		
	}

	private static void matrixTranspose(int[][] matrixResult) {
		int[][] s = new int[matrixResult[0].length][matrixResult.length];
		for (int i = 0; i < s.length; i++) {
			for(int j = 0; j < s[0].length; j++) {
				s[i][j] = matrixResult[j][i];
			}
		}
		showMatrix(s);
	}

	private static void showMatrix(int[][] s) {
		System.out.println("Maтрица : " + s.length
				+ " на " + s[0].length);
		for (int i = 0; i < s.length; i++) {
			for (int j = 0; j < s[0].length; j++)
				System.out.print(s[i][j] + " ");
			System.out.println();
		}
	}

	static int[][] matrixCreate() {
		int m, n;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter numbers of rows: ");
		m = sc.nextInt();
		System.out.print("Enter numbers of lines: ");
		n = sc.nextInt();
		System.out.println("You had created matrix: "+ m + "x" + n);
		int[][] s = new int[m][n];
		for (int i = 0; i < s.length; i++) {
			for(int j = 0; j < s[0].length; j++) {
				System.out.println("Enter [" + i + "][" + j + "] tuple of matrix s");
				s[i][j] = sc.nextInt();
			}
	    }
		
		showMatrix(s);
		
		return s;
		
	}
	

}
